package cn.edu.xmu.oomall.share.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.share.model.vo.LiquidationShareRetVo;
import cn.edu.xmu.oomall.share.service.ShareService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.Depart;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

@Api(value = "分享服务", tags = "分享服务")
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class ShareController {
    @Autowired
    private ShareService shareService;

    private LocalDateTime zonedDateTime2LocalDateTime(ZonedDateTime zonedDateTime) {
        if (zonedDateTime == null) {
            return null;
        } else {
            return zonedDateTime.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
        }
    }

    @PutMapping("/internal/beshared/{id}")
    InternalReturnObject updateBeSharedState(@PathVariable("id") Long id, @RequestParam Byte state) {
        return new InternalReturnObject(shareService.updateBeSharedState(id,state));
    }

    @GetMapping("/internal/beshared")
    InternalReturnObject<List<LiquidationShareRetVo>> getBesharedList(@RequestParam Long productId, @RequestParam Long onsaleId, @RequestParam Long customerId) {
        return new InternalReturnObject(shareService.getBesharedList(productId,onsaleId,customerId).getData());
    }

    @PutMapping("/internal/shares/{id}")
    InternalReturnObject addSharesQuantity(@PathVariable("id") Long id, @RequestParam Long quantity) {
        shareService.addSharesQuantity(id,quantity);
        return new InternalReturnObject();
    }

    @GetMapping("/internal/shares/{id}")
    InternalReturnObject<Long> getSharesQuantity(@PathVariable("id") Long id) {
        return new InternalReturnObject(shareService.getSharesQuantity(id).getData());
    }



    @ApiOperation(value = "分享者生成分享链接")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功")})
    @PostMapping("/onsales/{id}/shares")
    @Audit
    public Object generateShareResult(@LoginUser Long userId, @LoginName String userName, @PathVariable("id") Long id) {
        ReturnObject returnObject = shareService.generateShareResult(userId, userName, id);
        return Common.decorateReturnObject(returnObject);
    }

    @ApiOperation(value = "顾客查询所有自己的分享记录")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功")})
    @GetMapping("/shares")
    @Audit
    public Object getShares(@LoginUser Long userId, @LoginName String userName,
                            @RequestParam(required = false) Long productId,
                            @RequestParam(required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginDate,
                            @RequestParam(required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX")ZonedDateTime endDate,
                            @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        ReturnObject returnObject = shareService.getShares(userId, userName, productId,
                zonedDateTime2LocalDateTime(beginDate), zonedDateTime2LocalDateTime(endDate), page, pageSize);
        return Common.decorateReturnObject(returnObject);
    }


    @ApiOperation(value = "查看商品的详细信息（需登录，从分享模式查看商品）")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功")})
    @GetMapping("/shares/{sid}/products/{id}")
    @Audit
    public Object getProductInShareMode(@LoginUser Long userId, @PathVariable("sid") Long shareId, @PathVariable("id") Long id) {
        Object obj =  shareService.getProductInShareMode(userId, shareId, id);
        if (obj instanceof ReturnObject) {
            return Common.decorateReturnObject((ReturnObject) obj);
        }
        else {
            return obj;
        }
    }

    @ApiOperation(value = "管理员查询商品分享记录")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功")})
    @GetMapping("/shops/{did}/products/{id}/shares")
    @Audit
    public Object getSharesOfGoods(@PathVariable("did") Long shopId, @PathVariable("id") Long productId,
                                   @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        ReturnObject returnObject = shareService.getSharesOfGoods(shopId, productId, page, pageSize);
        return Common.decorateReturnObject(returnObject);
    }


    @ApiOperation(value = "分享者查询所有分享成功记录")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功")})
    @GetMapping("/beshared")
    @Audit
    public Object getBeShared(@LoginUser Long userId, @RequestParam(required = false) Long productId,
                              @RequestParam(required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginDate,
                              @RequestParam(required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX")ZonedDateTime endDate,
                              @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        ReturnObject returnObject = shareService.getBeShared(userId, productId,
                zonedDateTime2LocalDateTime(beginDate), zonedDateTime2LocalDateTime(endDate), page, pageSize);
        return Common.decorateReturnObject(returnObject);
    }

    @ApiOperation(value = "管理员查询所有分享成功记录")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功")})
    @GetMapping("/shops/{did}/products/{id}/beshared")
    @Audit(departName = "shops")
    public Object getAllBeShared(@PathVariable("did") Long shopId, @PathVariable("id") Long productId,
                                 @RequestParam(required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginDate,
                                 @RequestParam(required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endDate,
                                 @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                                 @Depart Long departId) {
        ReturnObject returnObject = shareService.getAllBeShared(departId, productId,
                zonedDateTime2LocalDateTime(beginDate), zonedDateTime2LocalDateTime(endDate), page, pageSize);
        return Common.decorateReturnObject(returnObject);
    }
}
